<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>nexttick.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_async_nexttick.js.html">nexttick.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides a function to schedule running a function as soon
<a name="line17"></a> * as possible after the current JS execution stops and yields to the event
<a name="line18"></a> * loop.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.async.nextTick&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line25"></a>goog.require(&#39;goog.functions&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Fires the provided callbacks as soon as possible after the current JS
<a name="line30"></a> * execution context. setTimeout(?, 0) always takes at least 5ms for legacy
<a name="line31"></a> * reasons.
<a name="line32"></a> * @param {function(this:SCOPE)} callback Callback function to fire as soon as
<a name="line33"></a> *     possible.
<a name="line34"></a> * @param {SCOPE=} opt_context Object in whose scope to call the listener.
<a name="line35"></a> * @template SCOPE
<a name="line36"></a> */
<a name="line37"></a>goog.async.nextTick = function(callback, opt_context) {
<a name="line38"></a>  var cb = callback;
<a name="line39"></a>  if (opt_context) {
<a name="line40"></a>    cb = goog.bind(callback, opt_context);
<a name="line41"></a>  }
<a name="line42"></a>  cb = goog.async.nextTick.wrapCallback_(cb);
<a name="line43"></a>  // Introduced and currently only supported by IE10.
<a name="line44"></a>  if (goog.isFunction(goog.global.setImmediate)) {
<a name="line45"></a>    goog.global.setImmediate(cb);
<a name="line46"></a>    return;
<a name="line47"></a>  }
<a name="line48"></a>  // Look for and cache the custom fallback version of setImmediate.
<a name="line49"></a>  if (!goog.async.nextTick.setImmediate_) {
<a name="line50"></a>    goog.async.nextTick.setImmediate_ =
<a name="line51"></a>        goog.async.nextTick.getSetImmediateEmulator_();
<a name="line52"></a>  }
<a name="line53"></a>  goog.async.nextTick.setImmediate_(cb);
<a name="line54"></a>};
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * Cache for the setImmediate implementation.
<a name="line59"></a> * @type {function(function())}
<a name="line60"></a> * @private
<a name="line61"></a> */
<a name="line62"></a>goog.async.nextTick.setImmediate_;
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * Determines the best possible implementation to run a function as soon as
<a name="line67"></a> * the JS event loop is idle.
<a name="line68"></a> * @return {function(function())} The &quot;setImmediate&quot; implementation.
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.async.nextTick.getSetImmediateEmulator_ = function() {
<a name="line72"></a>  // If native Promises are available in the browser, just schedule the callback
<a name="line73"></a>  // on a fulfilled promise, which is specified to be async, but as fast as
<a name="line74"></a>  // possible.
<a name="line75"></a>  if (goog.global.Promise &amp;&amp; goog.global.Promise.resolve) {
<a name="line76"></a>    var promise = goog.global.Promise.resolve();
<a name="line77"></a>    return function(cb) {
<a name="line78"></a>      promise.then(cb);
<a name="line79"></a>    };
<a name="line80"></a>  }
<a name="line81"></a>  // Create a private message channel and use it to postMessage empty messages
<a name="line82"></a>  // to ourselves.
<a name="line83"></a>  var Channel = goog.global[&#39;MessageChannel&#39;];
<a name="line84"></a>  // If MessageChannel is not available and we are in a browser, implement
<a name="line85"></a>  // an iframe based polyfill in browsers that have postMessage and
<a name="line86"></a>  // document.addEventListener. The latter excludes IE8 because it has a
<a name="line87"></a>  // synchronous postMessage implementation.
<a name="line88"></a>  if (typeof Channel === &#39;undefined&#39; &amp;&amp; typeof window !== &#39;undefined&#39; &amp;&amp;
<a name="line89"></a>      window.postMessage &amp;&amp; window.addEventListener) {
<a name="line90"></a>    /** @constructor */
<a name="line91"></a>    Channel = function() {
<a name="line92"></a>      // Make an empty, invisible iframe.
<a name="line93"></a>      var iframe = document.createElement(&#39;iframe&#39;);
<a name="line94"></a>      iframe.style.display = &#39;none&#39;;
<a name="line95"></a>      iframe.src = &#39;&#39;;
<a name="line96"></a>      document.documentElement.appendChild(iframe);
<a name="line97"></a>      var win = iframe.contentWindow;
<a name="line98"></a>      var doc = win.document;
<a name="line99"></a>      doc.open();
<a name="line100"></a>      doc.write(&#39;&#39;);
<a name="line101"></a>      doc.close();
<a name="line102"></a>      var message = &#39;callImmediate&#39; + Math.random();
<a name="line103"></a>      var origin = win.location.protocol + &#39;//&#39; + win.location.host;
<a name="line104"></a>      var onmessage = goog.bind(function(e) {
<a name="line105"></a>        // Validate origin and message to make sure that this message was
<a name="line106"></a>        // intended for us.
<a name="line107"></a>        if (e.origin != origin &amp;&amp; e.data != message) {
<a name="line108"></a>          return;
<a name="line109"></a>        }
<a name="line110"></a>        this[&#39;port1&#39;].onmessage();
<a name="line111"></a>      }, this);
<a name="line112"></a>      win.addEventListener(&#39;message&#39;, onmessage, false);
<a name="line113"></a>      this[&#39;port1&#39;] = {};
<a name="line114"></a>      this[&#39;port2&#39;] = {
<a name="line115"></a>        postMessage: function() {
<a name="line116"></a>          win.postMessage(message, origin);
<a name="line117"></a>        }
<a name="line118"></a>      };
<a name="line119"></a>    };
<a name="line120"></a>  }
<a name="line121"></a>  if (typeof Channel !== &#39;undefined&#39;) {
<a name="line122"></a>    var channel = new Channel();
<a name="line123"></a>    // Use a fifo linked list to call callbacks in the right order.
<a name="line124"></a>    var head = {};
<a name="line125"></a>    var tail = head;
<a name="line126"></a>    channel[&#39;port1&#39;].onmessage = function() {
<a name="line127"></a>      head = head.next;
<a name="line128"></a>      var cb = head.cb;
<a name="line129"></a>      head.cb = null;
<a name="line130"></a>      cb();
<a name="line131"></a>    };
<a name="line132"></a>    return function(cb) {
<a name="line133"></a>      tail.next = {
<a name="line134"></a>        cb: cb
<a name="line135"></a>      };
<a name="line136"></a>      tail = tail.next;
<a name="line137"></a>      channel[&#39;port2&#39;].postMessage(0);
<a name="line138"></a>    };
<a name="line139"></a>  }
<a name="line140"></a>  // Implementation for IE6-8: Script elements fire an asynchronous
<a name="line141"></a>  // onreadystatechange event when inserted into the DOM.
<a name="line142"></a>  if (typeof document !== &#39;undefined&#39; &amp;&amp; &#39;onreadystatechange&#39; in
<a name="line143"></a>      document.createElement(&#39;script&#39;)) {
<a name="line144"></a>    return function(cb) {
<a name="line145"></a>      var script = document.createElement(&#39;script&#39;);
<a name="line146"></a>      script.onreadystatechange = function() {
<a name="line147"></a>        // Clean up and call the callback.
<a name="line148"></a>        script.onreadystatechange = null;
<a name="line149"></a>        script.parentNode.removeChild(script);
<a name="line150"></a>        script = null;
<a name="line151"></a>        cb();
<a name="line152"></a>        cb = null;
<a name="line153"></a>      };
<a name="line154"></a>      document.documentElement.appendChild(script);
<a name="line155"></a>    };
<a name="line156"></a>  }
<a name="line157"></a>  // Fall back to setTimeout with 0. In browsers this creates a delay of 5ms
<a name="line158"></a>  // or more.
<a name="line159"></a>  return function(cb) {
<a name="line160"></a>    goog.global.setTimeout(cb, 0);
<a name="line161"></a>  };
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Helper function that is overrided to protect callbacks with entry point
<a name="line167"></a> * monitor if the application monitors entry points.
<a name="line168"></a> * @param {function()} callback Callback function to fire as soon as possible.
<a name="line169"></a> * @return {function()} The wrapped callback.
<a name="line170"></a> * @private
<a name="line171"></a> */
<a name="line172"></a>goog.async.nextTick.wrapCallback_ = goog.functions.identity;
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>// Register the callback function as an entry point, so that it can be
<a name="line176"></a>// monitored for exception handling, etc. This has to be done in this file
<a name="line177"></a>// since it requires special code to handle all browsers.
<a name="line178"></a>goog.debug.entryPointRegistry.register(
<a name="line179"></a>    /**
<a name="line180"></a>     * @param {function(!Function): !Function} transformer The transforming
<a name="line181"></a>     *     function.
<a name="line182"></a>     */
<a name="line183"></a>    function(transformer) {
<a name="line184"></a>      goog.async.nextTick.wrapCallback_ = transformer;
<a name="line185"></a>    });
</pre>


</body>
</html>
